function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// var rootDocment = 'https://ymt.yimaxtx.com/wechat';
// var rootDocment = 'https://ymt.yimating.com/wechat';
// var rootDocment = 'https://kwz.hztuen.com/wechat';
var rootDocment = 'https://ymt.yimating.cn/wechat';

function req(url, data, cb) {
  wx.request({
    url: rootDocment + url,
    data: data,
    method: 'post',
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      return typeof cb == "function" && cb(res.data)
    },
    fail: function(res) {
      console.log(res);
      // return typeof cb == "function" && cb(false)
      // wx.showToast({
      //   title: '请求超时！',
      //   icon: 'none'
      // })
    }
  })
}

function getReq(url, data, cb) {
  wx.request({
    url: rootDocment + url,
    data: data,
    method: 'get',
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      return typeof cb == "function" && cb(res.data)
    },
    fail: function() {
      return typeof cb == "function" && cb(false)
    }
  })
}

// 去前后空格  
function trim(str) {
  return str.replace(/(^\s*)|(\s*$)/g, "");
}

// 提示错误信息  
function isError(msg, that) {
  that.setData({
    showTopTips: true,
    errorMsg: msg
  })
}

// 清空错误信息  
function clearError(that) {
  that.setData({
    showTopTips: false,
    errorMsg: ""
  })
}
//时间戳转换为时间
function formatTime(time) {
  var date = new Date(time);
  var year = date.getFullYear();
  var month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
  var date1 = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return year + "-" + month + "-" + date1 + " " + hour + ":" + minute + ":" + second;
}
//时间戳转换为年月日
function formatTime_year(time) {
  var date = new Date(time);
  var year = date.getFullYear();
  var month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
  var date1 = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  return year + "-" + month + "-" + date1;
}
//时间戳转换为时分秒
function time(time) {
  var date = new Date(time);
  var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return hour + ":" + minute + ":" + second;
}
//秒数转换为时分秒
function formatSeconds(value) {
  var secondTime = parseInt(value); // 秒
  var minuteTime = 0; // 分
  var hourTime = 0; // 小时
  if (secondTime > 60) { //如果秒数大于60，将秒数转换成整数
    //获取分钟，除以60取整数，得到整数分钟
    minuteTime = parseInt(secondTime / 60);
    //获取秒数，秒数取佘，得到整数秒数
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60，将分钟转换成小时
    if (minuteTime > 60) {
      //获取小时，获取分钟除以60，得到整数小时
      hourTime = parseInt(minuteTime / 60);
      //获取小时后取佘的分，获取分钟除以60取佘的分
      minuteTime = parseInt(minuteTime % 60);
    }
  }
  if (secondTime >= 0 && secondTime < 10) {
    var result = "0" + parseInt(secondTime) + "";
  } else {
    var result = "" + parseInt(secondTime) + "";
  }
  if (minuteTime >= 0 && minuteTime < 10) {
    result = "0" + parseInt(minuteTime) + ":" + result;
  } else {
    result = "" + parseInt(minuteTime) + ":" + result;
  }
  if (hourTime >= 0 && hourTime < 10) {
    result = "0" + parseInt(hourTime) + ":" + result;
  } else {
    result = "" + parseInt(hourTime) + ":" + result;
  }
  return result;
}
const jump = (evt, num) => {
  var {
    openType,
    url
  } = evt.currentTarget.dataset;
  if (openType != 'navigateBack') {
    wx[openType || 'navigateTo']({
      url
    })
  } else {
    wx.navigateBack({
      delta: num
    })
  }
}
module.exports = {
  formatTime: formatTime,
  req: req,
  trim: trim,
  isError: isError,
  clearError: clearError,
  getReq: getReq,
  formatTime: formatTime,
  rootDocment: rootDocment,
  time: time,
  formatSeconds: formatSeconds,
  formatTime_year: formatTime_year,
  jump
}